package DRC;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import DRCOMM.DRHTTP_API;
import DRCOMM.DR_SESSION;
import DRCOMM.FJW_URI;
import DRCOMM.dr_ajax_answer;
import DRCOMM.dr_util;
import DRCOMM.record_data_set;

public class dataItem {

    public int channel_id=-1;
    public int dataItem_id=-1;
    public int slave_id=-1;
    public int register_type=-1;
    public int data_index=-1;
    public int address=-1;
    public String label="";
    public int data_type=-1;
    public float coefficient=-1;
    public int negation=-1;
    public int BCD=-1;
    public int ABS=-1;
    public float base_value=-1;
    public String introduction="";
    public String data_table="";
    public String field="";
    public float value=-1.0f;
    public Date timestamp=new Date();
    public Date create_time=new Date();
    public static defaultDataItems ddis=new defaultDataItems(); //default data items
    public static ArrayList<channel> channels=null;
    public static ArrayList<channel4collect> channels4collect=null;

    public DRCDB drcdb;

    public dataItem(DRCDB drcdb){
        this.drcdb=drcdb;
    }

    public dataItem(DRCDB drcdb,int channel_id,int slave_id,int register_type,
        int data_index,int address,String label,int data_type){
        this.drcdb=drcdb;
        this.channel_id=channel_id;
        this.slave_id=slave_id;
        this.register_type=register_type;
        this.data_index=data_index;
        this.address=address;
        this.label=label;
        this.data_type=data_type;
    }

    public dataItem(int dataItem_id,int channel_id,int slave_id,int register_type,
        int data_index,int address,String label,int data_type){
        this.dataItem_id=dataItem_id;
        this.channel_id=channel_id;
        this.slave_id=slave_id;
        this.register_type=register_type;
        this.data_index=data_index;
        this.address=address;
        this.label=label;
        this.data_type=data_type;
    }

    public dataItem(int channel_id,int dataItem_id,int slave_id,int register_type,
        int data_index,int address,String label,int data_type,float coefficient,int negation,
        int BCD,int ABS,float base_value,String introduction,String data_table,String field,
        float value,Date timestamp,Date create_time){
        this.channel_id=channel_id;
        this.dataItem_id=dataItem_id;
        this.slave_id=slave_id;
        this.register_type=register_type;
        this.data_index=data_index;
        this.address=address;
        this.label=label;
        this.data_type=data_type;
        this.coefficient=coefficient;
        this.negation=negation;
        this.BCD=BCD;
        this.ABS=ABS;
        this.base_value=base_value;
        this.introduction=introduction;
        this.data_table=data_table;
        this.field=field;
        this.value=value;
        this.timestamp=timestamp;
        this.create_time=create_time;
    }

    public void report(){
        System.out.println("channel_id="+this.channel_id);
        System.out.println("dataItem_id="+this.dataItem_id);
        System.out.println("slave_id="+this.slave_id);
        System.out.println("register_type="+this.register_type);
        System.out.println("data_index="+this.data_index);
        System.out.println("address="+this.address);
        System.out.println("label="+this.label);
        System.out.println("data_type="+this.data_type);
        System.out.println("coefficient="+this.coefficient);
        System.out.println("negation="+this.negation);
        System.out.println("BCD="+this.BCD);
        System.out.println("ABS="+this.ABS);
        System.out.println("base_value="+this.base_value);
        System.out.println("introduction="+this.introduction);
        System.out.println("data_table="+this.data_table);
        System.out.println("field="+this.field);
        System.out.println("value="+this.value);
        System.out.println("timestamp="+this.timestamp);
        System.out.println("create_time="+this.create_time);
    }

    public record_data_set dataItems2rds(List<dataItem> dataItems,List<String> columns){
        record_data_set rds=new record_data_set();
        rds.columns=columns;
        rds.count=dataItems.size();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        for(dataItem di:dataItems){
            Map<String,String> row=new HashMap<String, String>();
            for(String col:columns){
                Field f;
                try {
                    f = di.getClass().getDeclaredField(col);
                    f.setAccessible(true);
                    String value=null;
                    if(f.getType().getName().equals("int")||f.getType().getName().equals("float")){
                        try {
                            value=(String)f.get(di);
                        } catch (IllegalArgumentException | IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    if(f.getType().getName().equals("String")){
                        try {
                            value=(String)f.get(di);
                        } catch (IllegalArgumentException | IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    if(f.getType().getName().equals("Date")){
                        try {
                            value=ft.format((Date)f.get(di));
                        } catch (IllegalArgumentException | IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    row.put(col,value);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                }
            }
            rds.datas.add(row);
        }
        return rds;
    }

    public void set_dataItem_id(int dataItem_id){
        this.dataItem_id=dataItem_id;
    }

    public record_data_set defaultDataItem(int channel_id,int slave_id,int register_type,
        int pagesize,int page){
        // DRCDB db=this.drcdb;
        // String sqlstr="SELECT d1.data_index,d1.value value1,d2.value value2,d3.value value3,d4.value value4\n"+
        // "from dataItem d1\n"+
        // "left join dataItem d2\n"+
        // "on d1.data_index=d2.data_index\n"+
        // "left join dataItem d3\n"+
        // "on d1.data_index=d3.data_index\n"+
        // "left join dataItem d4\n"+
        // "on d1.data_index=d4.data_index\n"+
        // "where d1.register_type=1 \n"+
        // "and d2.register_type=2\n"+
        // "and d3.register_type=3 \n"+
        // "and d4.register_type=4\n"+
        // "and d1.channel_id=1\n"+
        // "and d2.channel_id=1\n"+
        // "and d3.channel_id=1\n"+
        // "and d4.channel_id=1\n"+
        // "order by d1.data_index";
        // sqlstr+=String.format(" limit %d,%d",pagesize*(page-1),pagesize);
        // record_data_set rds=db.executeQuerySQL(sqlstr);
        // sqlstr="SELECT count(1) as ct\n"+
        // "from dataItem d1\n"+
        // "left join dataItem d2\n"+
        // "on d1.data_index=d2.data_index\n"+
        // "left join dataItem d3\n"+
        // "on d1.data_index=d3.data_index\n"+
        // "left join dataItem d4\n"+
        // "on d1.data_index=d4.data_index\n"+
        // "where d1.register_type=1 \n"+
        // "and d2.register_type=2\n"+
        // "and d3.register_type=3 \n"+
        // "and d4.register_type=4\n"+
        // "and d1.channel_id=1\n"+
        // "and d2.channel_id=1\n"+
        // "and d3.channel_id=1\n"+
        // "and d4.channel_id=1\n";
        // rds.count=db.get_query_count(sqlstr);
        record_data_set rds=new record_data_set();
        List<String> columns=new ArrayList<String>();
        columns.add("data_index");
        columns.add("value1");
        columns.add("value2");
        columns.add("value3");
        columns.add("value4");
        rds.columns=columns;
        List<Map<String,String>> datas=new ArrayList<Map<String,String>>();
        ArrayList<dataItem> dataItems=channels.get(0).dataItems;
        for(int i=0;i<channel.data_count;i++){
            Map<String,String> row=new HashMap<String, String>();
            row.put("data_index",dataItems.get(i).data_index+"");
            row.put("value1",dataItems.get(i).value+"");
            row.put("value2",dataItems.get(i+defaultDataItems.data_count).value+"");
            row.put("value3",dataItems.get(i+defaultDataItems.data_count*2).value+"");
            row.put("value4",dataItems.get(i+defaultDataItems.data_count*3).value+"");
            datas.add(row);
        }
        rds.count=defaultDataItems.data_count;
        rds.datas=datas;
        return rds;
    }

    public record_data_set read_dataItems(int channel_id,int slave_id,int register_type,
        int start_address,int length){
        // DRCDB db=this.drcdb;
        // String sqlstr="select * from dataItem ";
        // sqlstr+=" where channel_id="+channel_id;
        // sqlstr+=" and slave_id="+slave_id;
        // sqlstr+=" and register_type="+register_type;
        // sqlstr+=" and address>="+start_address;
        // sqlstr+=" order by address";
        // sqlstr+=" limit "+length;
        // record_data_set rds=db.executeQuerySQL(sqlstr);
        // sqlstr="select count(1) as ct from dataItem ";
        // sqlstr+=" where channel_id="+channel_id;
        // sqlstr+=" and slave_id="+slave_id;
        // sqlstr+=" and register_type="+register_type;
        // sqlstr+=" and address>="+start_address;
        // rds.count=db.get_query_count(sqlstr);
        channel ch=null;
        for(int i=0;i<channels.size();i++){
            if(channels.get(i).channel_id==channel_id){
                ch=channels.get(i);
            }
        }
        if(ch==null)return null;
        record_data_set rds=new record_data_set();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        List<String> columns=new ArrayList<String>();
        columns.add("channel_id");
        columns.add("dataItem_id");
        columns.add("slave_id");
        columns.add("register_type");
        columns.add("data_index");
        columns.add("address");
        columns.add("label");
        columns.add("data_type");
        columns.add("coefficient");
        columns.add("negation");
        columns.add("BCD");
        columns.add("ABS");
        columns.add("base_value");
        columns.add("introduction");
        columns.add("data_table");
        columns.add("field");
        columns.add("value");
        columns.add("timestamp");
        columns.add("create_time");
        rds.columns=columns;
        List<Map<String,String>> datas=new ArrayList<Map<String,String>>();
        dataItem di;
        for(int i=0;i<ch.dataItems.size();i++){
            di=ch.dataItems.get(i);
            if(di.slave_id==slave_id&&di.register_type==register_type&&di.address==start_address){
                for(int j=0;j<length&&j<ch.dataItems.size();j++){
                    di=ch.dataItems.get(j);
                    Map<String,String> row=new HashMap<String, String>();
                    row.put("channel_id",""+di.channel_id);
                    row.put("dataItem_id",""+di.dataItem_id);
                    row.put("slave_id",""+di.slave_id);
                    row.put("register_type",""+di.register_type);
                    row.put("data_index",""+di.data_index);
                    row.put("address",""+di.address);
                    row.put("label",""+di.label);
                    row.put("data_type",""+di.data_type);
                    row.put("coefficient",""+di.coefficient);
                    row.put("negation",""+di.negation);
                    row.put("BCD",""+di.BCD);
                    row.put("ABS",""+di.ABS);
                    row.put("base_value",""+di.base_value);
                    row.put("introduction",""+di.introduction);
                    row.put("data_table",""+di.data_table);
                    row.put("field",""+di.field);
                    row.put("value",""+di.value);
                    row.put("timestamp",""+ft.format(di.timestamp));
                    row.put("create_time",""+ft.format(di.create_time));
                    datas.add(row);
                }
                break;
            }
        }
        rds.count=ch.dataItems.size();
        rds.datas=datas;
        return rds;
    }

    public static byte[] readLocalDataItem4bit(int slave_id,int register_type,
        int data_index,int data_count){
        List<Byte> result=new ArrayList<Byte>();
        // String sqlstr="select value from dataItem";
        // sqlstr+=String.format(" where channel_id=1 and slave_id=%d" , slave_id);
        // sqlstr+=String.format(" and register_type=%d" , register_type);
        // sqlstr+=String.format(" and data_index>=%d" , data_index);
        // sqlstr+=String.format(" order by data_index");
        // sqlstr+=String.format(" limit 0,%d" , data_count);
        // record_data_set rds=drcdb.executeQuerySQL(sqlstr);
        channel ch=channels.get(0);
        record_data_set rds=new record_data_set();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        List<String> columns=new ArrayList<String>();
        columns.add("channel_id");
        columns.add("dataItem_id");
        columns.add("slave_id");
        columns.add("register_type");
        columns.add("data_index");
        columns.add("address");
        columns.add("label");
        columns.add("data_type");
        columns.add("coefficient");
        columns.add("negation");
        columns.add("BCD");
        columns.add("ABS");
        columns.add("base_value");
        columns.add("introduction");
        columns.add("data_table");
        columns.add("field");
        columns.add("value");
        columns.add("timestamp");
        columns.add("create_time");
        rds.columns=columns;
        List<Map<String,String>> datas=new ArrayList<Map<String,String>>();
        dataItem di;
        for(int i=0;i<ch.dataItems.size();i++){
            di=ch.dataItems.get(i);
            if(di.slave_id==slave_id&&di.register_type==register_type&&di.data_index==data_index){
                for(int j=0;j<data_count&&j<ch.dataItems.size();j++){
                    di=ch.dataItems.get(j);
                    Map<String,String> row=new HashMap<String, String>();
                    row.put("channel_id",""+di.channel_id);
                    row.put("dataItem_id",""+di.dataItem_id);
                    row.put("slave_id",""+di.slave_id);
                    row.put("register_type",""+di.register_type);
                    row.put("data_index",""+di.data_index);
                    row.put("address",""+di.address);
                    row.put("label",""+di.label);
                    row.put("data_type",""+di.data_type);
                    row.put("coefficient",""+di.coefficient);
                    row.put("negation",""+di.negation);
                    row.put("BCD",""+di.BCD);
                    row.put("ABS",""+di.ABS);
                    row.put("base_value",""+di.base_value);
                    row.put("introduction",""+di.introduction);
                    row.put("data_table",""+di.data_table);
                    row.put("field",""+di.field);
                    row.put("value",""+di.value);
                    row.put("timestamp",""+ft.format(di.timestamp));
                    row.put("create_time",""+ft.format(di.create_time));
                    datas.add(row);
                }
                break;
            }
        }
        rds.count=channel.data_count;
        rds.datas=datas;
        byte b;
        rds.report();
        for(int i=0;i<rds.datas.size();i+=8){
            b=0;
            for(int j=i;j<(i+8)&&j<rds.datas.size();j++){
                if(Float.parseFloat(rds.datas.get(j).get("value"))==1.0f){
                    b=(byte) ((byte)b|0x01<<(j-i));
                }
            }
            result.add(b);
        }
        // // 翻转result 列表
        // Collections.sort(result,Collections.reverseOrder());
        return dr_util.ListToBytes(result);
    }

    public static record_data_set readLocalDataItem(DRCDB drcdb,int slave_id,int register_type,
        int data_index,int data_count){
        // String sqlstr="select value from dataItem";
        // sqlstr+=String.format(" where channel_id=1 and slave_id=%d" , slave_id);
        // sqlstr+=String.format(" and register_type=%d" , register_type);
        // sqlstr+=String.format(" and data_index>=%d" , data_index);
        // sqlstr+=String.format(" order by data_index");
        // sqlstr+=String.format(" limit 0,%d" , data_count);
        // record_data_set rds=drcdb.executeQuerySQL(sqlstr);
        channel ch=channels.get(0);
        record_data_set rds=new record_data_set();
        List<String> columns=new ArrayList<String>();
        columns.add("value");
        rds.columns=columns;
        List<Map<String,String>> datas=new ArrayList<Map<String,String>>();
        dataItem di;
        for(int i=0;i<ch.dataItems.size();i++){
            di=ch.dataItems.get(i);
            if(di.slave_id==slave_id&&di.register_type==register_type&&di.data_index==data_index){
                for(int j=0;j<data_count&&j<ch.dataItems.size();j++){
                    di=ch.dataItems.get(j);
                    Map<String,String> row=new HashMap<String, String>();
                    row.put("value",""+di.value);
                    datas.add(row);
                }
                break;
            }
        }
        rds.count=channel.data_count;
        rds.datas=datas;
        return rds;
    }

    public static ArrayList<Float> readLocalDataItem(int slave_id,int register_type,
        int data_index,int data_count){
        ArrayList<Float> dis=new ArrayList<Float>();
        ArrayList<dataItem> dataItems=channels.get(0).dataItems;
        // 针对本地寄存器，可直接访问以加快速度，暂时屏蔽以下代码
        // 2024年6月14日 罗继东
        // // 逐个扫描数据点，判断slave_id,register_type,data_index是否一致
        // for(dataItem di:channels.get(0).dataItems){
        //     if(slave_id==di.slave_id&&register_type==di.register_type&&data_index==di.data_index){
        //         for(int i=0;i<data_count&&i<dataItems.size();i++){
        //             dis.add(dataItems.get(i+di.dataItem_id).value);
        //         }
        //     }
        // }

        // 根据地址直接访问
        dataItem di;
        for(int i=0;i<data_count&&i<dataItems.size();i++){
            di=dataItems.get(i+(register_type-1)*channel.data_count);
            dis.add(di.value);
        }
        return dis;
    }

    public static boolean writeLocalDataItem4bit(DRCDB drcdb,int slave_id,int register_type,
        int address,int value){
        boolean result=false;
        int bit_value=0;
        if(value==0xFF00)bit_value=1;
        Date now=new Date();
        // SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String sqlstr="update dataItem";
        // sqlstr+=String.format(" set timestamp='%s'" ,ft.format(now));
        // sqlstr+=String.format(",value=%d" ,bit_value);
        // sqlstr+=String.format(" where channel_id=1 and slave_id=%d" , slave_id);
        // sqlstr+=String.format(" and register_type=%d" , register_type);
        // sqlstr+=String.format(" and address=%d" , address);
        // result=drcdb.executeUpdateSQL(sqlstr);
        channel ch=channels.get(0);
        for(int i=0;i<ch.dataItems.size();i++){
            dataItem di=ch.dataItems.get(i);
            if(di.slave_id==slave_id&&di.register_type==register_type&&di.address==address){
                di.value=bit_value;
                di.timestamp=now;
                result=true;
                break;
            }
        }
        return result;
    }

     public static boolean writeLocalDataItem(DRCDB drcdb,int slave_id,int register_type,
        int address,float value){
        boolean result=false;
        Date now=new Date();
        // SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        // String sqlstr="update dataItem";
        // sqlstr+=String.format(" set timestamp='%s'" ,ft.format(now));
        // sqlstr+=String.format(",value=%f" ,value);
        // sqlstr+=String.format(" where channel_id=1 and slave_id=%d" , slave_id);
        // sqlstr+=String.format(" and register_type=%d" , register_type);
        // sqlstr+=String.format(" and address=%d" , address);
        // result=drcdb.executeUpdateSQL(sqlstr);
        channel ch=channels.get(0);
        for(int i=0;i<ch.dataItems.size();i++){
            dataItem di=ch.dataItems.get(i);
            if(di.slave_id==slave_id&&di.register_type==register_type&&di.address==address){
                di.value=value;
                di.timestamp=now;
                result=true;
                break;
            }
        }
        return result;
    }   

    public static boolean writeLocalDataItem(int slave_id,int register_type,
    int address,float value){
        boolean result=false;
        dataItem di=channels.get(0).dataItems.get(address);
        di.value=value;
        di.timestamp=new Date();
        return result;
    }   

    public record_data_set read_dataItems_page(int channel_id,int slave_id,int register_type,
        int pagesize,int page){
        // DRCDB db=this.drcdb;
        // String sqlstr="select * from dataItem ";
        // sqlstr+=" where channel_id="+channel_id;
        // sqlstr+=" and slave_id="+slave_id;
        // sqlstr+=" and register_type="+register_type;
        // sqlstr+=" order by address";
        // sqlstr+=String.format(" limit %d,%d",pagesize*(page-1),pagesize);
        // record_data_set rds=db.executeQuerySQL(sqlstr);
        // sqlstr="select count(1) as ct from dataItem ";
        // sqlstr+=" where channel_id="+channel_id;
        // sqlstr+=" and slave_id="+slave_id;
        // sqlstr+=" and register_type="+register_type;
        // rds.count=db.get_query_count(sqlstr);
        channel ch=null;
        for(int i=0;i<channels.size();i++){
            if(channels.get(i).channel_id==channel_id){
                ch=channels.get(i);
            }
        }
        if(ch==null)return null;
        record_data_set rds=new record_data_set();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        List<String> columns=new ArrayList<String>();
        columns.add("channel_id");
        columns.add("dataItem_id");
        columns.add("slave_id");
        columns.add("register_type");
        columns.add("data_index");
        columns.add("address");
        columns.add("label");
        columns.add("data_type");
        columns.add("coefficient");
        columns.add("negation");
        columns.add("BCD");
        columns.add("ABS");
        columns.add("base_value");
        columns.add("introduction");
        columns.add("data_table");
        columns.add("field");
        columns.add("value");
        columns.add("timestamp");
        columns.add("create_time");
        rds.columns=columns;
        List<Map<String,String>> datas=new ArrayList<Map<String,String>>();
        dataItem di;
        for(int j=pagesize*(page-1);j<pagesize*page&&j<ch.dataItems.size();j++){
            di=ch.dataItems.get(j);
            Map<String,String> row=new HashMap<String, String>();
            row.put("channel_id",""+di.channel_id);
            row.put("dataItem_id",""+di.dataItem_id);
            row.put("slave_id",""+di.slave_id);
            row.put("register_type",""+di.register_type);
            row.put("data_index",""+di.data_index);
            row.put("address",""+di.address);
            row.put("label",""+di.label);
            row.put("data_type",""+di.data_type);
            row.put("coefficient",""+di.coefficient);
            row.put("negation",""+di.negation);
            row.put("BCD",""+di.BCD);
            row.put("ABS",""+di.ABS);
            row.put("base_value",""+di.base_value);
            row.put("introduction",""+di.introduction);
            row.put("data_table",""+di.data_table);
            row.put("field",""+di.field);
            row.put("value",""+di.value);
            row.put("timestamp",""+ft.format(di.timestamp));
            row.put("create_time",""+ft.format(di.create_time));
            datas.add(row);
        }
        rds.count=ch.dataItems.size();
        rds.datas=datas;
        return rds;
    }

    /**
     * 关联WEB API列表
     * @param api_lists
     * @param drcdb
     */
    public static void associate_actions(List<DRHTTP_API> api_lists,DRCDB drcdb) throws ClassNotFoundException{
        @SuppressWarnings("unchecked")
        Class<?> CLS=(Class<dataItem>) Class.forName("DRC.dataItem");
        for(Method method:CLS.getDeclaredMethods()){
            FJW_URI fjw_uri=method.getAnnotation(FJW_URI.class);
//            System.out.println(method.toString());
            if(fjw_uri!=null){
//                System.out.println(method.toString());
//                System.out.println(fjw_uri.value());
                try {
                    api_lists.add((DRHTTP_API)method.invoke(CLS,drcdb, fjw_uri.value()));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @FJW_URI(value ="dataItem/defaultDataItemList")
    public static DRHTTP_API defaultDataItemList(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                String page=this.get_parameters(parameters,"page");
                String limit=this.get_parameters(parameters,"limit");
                dataItem di=new dataItem(drcdb);
                record_data_set rds=di.defaultDataItem(1,1,3,Integer.parseInt(limit),Integer.parseInt(page));
                content=record_data_set.get_answer_json_str(0,"",rds);
                return content;
            }
        };
    }

    @FJW_URI(value ="dataItem/dataItemList")
    public static DRHTTP_API dataItemList(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                String page=this.get_parameters(parameters,"page");
                String limit=this.get_parameters(parameters,"limit");
                dataItem di=new dataItem(drcdb);
                record_data_set rds=di.read_dataItems_page(1,1,3,Integer.parseInt(limit),Integer.parseInt(page));
                content=record_data_set.get_answer_json_str(0,"",rds);
                return content;
            }
        };
    }

    @FJW_URI(value ="dataItem/readDataItem")
    public static DRHTTP_API readDataItem(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                String dataItem_id=this.get_parameters(parameters,"dataItem_id");
                // DRCDB db=drcdb;
                // String sqlstr="select * from dataItem ";
                // sqlstr+=" where dataItem_id="+dataItem_id;
                // record_data_set rds=db.executeQuerySQL(sqlstr);
                channel ch=null;
                dataItem di=null;
                for(int i=0;i<channels.size();i++){
                    for(int j=0;j<channels.get(i).dataItems.size();j++){    
                        if(channels.get(i).dataItems.get(j).dataItem_id==Integer.parseInt(dataItem_id)){
                            ch=channels.get(i);
                            di=ch.dataItems.get(j);
                            break;
                        }
                    }
                    if(di!=null)break;
                }
                if(ch==null)return null;
                record_data_set rds=new record_data_set();
                SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
                List<String> columns=new ArrayList<String>();
                columns.add("channel_id");
                columns.add("dataItem_id");
                columns.add("slave_id");
                columns.add("register_type");
                columns.add("data_index");
                columns.add("address");
                columns.add("label");
                columns.add("data_type");
                columns.add("coefficient");
                columns.add("negation");
                columns.add("BCD");
                columns.add("ABS");
                columns.add("base_value");
                columns.add("introduction");
                columns.add("data_table");
                columns.add("field");
                columns.add("value");
                columns.add("timestamp");
                columns.add("create_time");
                rds.columns=columns;
                List<Map<String,String>> datas=new ArrayList<Map<String,String>>();
                if(di!=null){
                    Map<String,String> row=new HashMap<String, String>();
                    row.put("channel_id",""+di.channel_id);
                    row.put("dataItem_id",""+di.dataItem_id);
                    row.put("slave_id",""+di.slave_id);
                    row.put("register_type",""+di.register_type);
                    row.put("data_index",""+di.data_index);
                    row.put("address",""+di.address);
                    row.put("label",""+di.label);
                    row.put("data_type",""+di.data_type);
                    row.put("coefficient",""+di.coefficient);
                    row.put("negation",""+di.negation);
                    row.put("BCD",""+di.BCD);
                    row.put("ABS",""+di.ABS);
                    row.put("base_value",""+di.base_value);
                    row.put("introduction",""+di.introduction);
                    row.put("data_table",""+di.data_table);
                    row.put("field",""+di.field);
                    row.put("value",""+di.value);
                    row.put("timestamp",""+ft.format(di.timestamp));
                    row.put("create_time",""+ft.format(di.create_time));
                    datas.add(row);
                }
                rds.count=1;
                rds.datas=datas;
                content=record_data_set.get_answer_json_str(0,"",rds);
                return content;
            }
        };
    }

    @FJW_URI(value ="dataItem/readDefaultDataItem")
    public static DRHTTP_API readDefaultDataItem(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                int data_index=Integer.parseInt(this.get_parameters(parameters,"data_index"));
                // DRCDB db=drcdb;
                // String sqlstr="SELECT d1.data_index,d1.value value1,d2.value value2,d3.value value3,d4.value value4\n"+
                // "from dataItem d1\n"+
                // "left join dataItem d2\n"+
                // "on d1.data_index=d2.data_index\n"+
                // "left join dataItem d3\n"+
                // "on d1.data_index=d3.data_index\n"+
                // "left join dataItem d4\n"+
                // "on d1.data_index=d4.data_index\n"+
                // "where d1.register_type=1 \n"+
                // "and d2.register_type=2\n"+
                // "and d3.register_type=3 \n"+
                // "and d4.register_type=4\n"+
                // "and d1.channel_id=1\n"+
                // "and d2.channel_id=1\n"+
                // "and d3.channel_id=1\n"+
                // "and d4.channel_id=1\n"+
                // "and d1.data_index="+data_index;
                // record_data_set rds=db.executeQuerySQL(sqlstr);
                record_data_set rds=new record_data_set();
                List<String> columns=new ArrayList<String>();
                columns.add("data_index");
                columns.add("value1");
                columns.add("value2");
                columns.add("value3");
                columns.add("value4");
                rds.columns=columns;
                List<Map<String,String>> datas=new ArrayList<Map<String,String>>();
                Map<String,String> row=new HashMap<String, String>();
                ArrayList<dataItem> dataItems=channels.get(0).dataItems;
                row.put("data_index",dataItems.get(data_index).data_index+"");
                row.put("value1",dataItems.get(data_index).value+"");
                row.put("value2",dataItems.get(data_index+defaultDataItems.data_count).value+"");
                row.put("value3",dataItems.get(data_index+defaultDataItems.data_count*2).value+"");
                row.put("value4",dataItems.get(data_index+defaultDataItems.data_count*3).value+"");
                datas.add(row);
                rds.count=1;
                rds.datas=datas;
                content=record_data_set.get_answer_json_str(0,"",rds);
                return content;
            }
        };
    }

    @FJW_URI(value ="dataItem/saveDefaultDataItemValue")
    public static DRHTTP_API saveDefaultDataItemValue(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                String data_index=this.get_parameters(parameters,"data_index");
                String data1=this.get_parameters(parameters,"data1");
                String data2=this.get_parameters(parameters,"data2");
                String data3=this.get_parameters(parameters,"data3");
                String data4=this.get_parameters(parameters,"data4");
                // DRCDB db=drcdb;
                // String sqlstr;
                try {
                    // sqlstr="update dataItem";
                    // sqlstr+=String.format(" set value=%s" , data1);
                    // sqlstr+=String.format(" where register_type=1 and data_index=%s" , data_index);
                    // db.executeUpdateSQL(sqlstr);
                    // sqlstr="update dataItem";
                    // sqlstr+=String.format(" set value=%s" , data2);
                    // sqlstr+=String.format(" where register_type=2 and data_index=%s" , data_index);
                    // db.executeUpdateSQL(sqlstr);
                    // sqlstr="update dataItem";
                    // sqlstr+=String.format(" set value=%s" , data3);
                    // sqlstr+=String.format(" where register_type=3 and data_index=%s" , data_index);
                    // db.executeUpdateSQL(sqlstr);
                    // sqlstr="update dataItem";
                    // sqlstr+=String.format(" set value=%s" , data4);
                    // sqlstr+=String.format(" where register_type=4 and data_index=%s" , data_index);
                    // db.executeUpdateSQL(sqlstr);
                    channel ch=channels.get(0);
                    ch.dataItems.get(Integer.parseInt(data_index)).value=Float.parseFloat(data1);
                    ch.dataItems.get(Integer.parseInt(data_index)+channel.data_count).value=Float.parseFloat(data2);
                    ch.dataItems.get(Integer.parseInt(data_index)+channel.data_count*2).value=Float.parseFloat(data3);
                    ch.dataItems.get(Integer.parseInt(data_index)+channel.data_count*3).value=Float.parseFloat(data4);
                    content=dr_ajax_answer.sample_json_string(0,"");     
                } catch (Exception e) {
                    content=dr_ajax_answer.sample_json_string(1,"保存失败！");
                }
                return content;
            }
        };
    }
    /**
     * 读取寄存器值，用于http调用
     * 以json格式返回，包括code和msg，以及数据结果
     * @param drcdb
     * @return
     */
    @FJW_URI(value ="dataItem/readDataItem2json")
    public static DRHTTP_API readDataItem2json(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                String slave_id=this.get_parameters(parameters,"slave_id");
                String register_type=this.get_parameters(parameters,"register_type");
                String address=this.get_parameters(parameters,"address");
                DRCDB db=drcdb;
                String sqlstr="select * from dataItem ";
                sqlstr+=" where slave_id="+slave_id;
                sqlstr+=" and register_type="+register_type;
                sqlstr+=" and address="+address;
                try {
                    record_data_set rds=db.executeQuerySQL(sqlstr);      
                    content=record_data_set.get_answer_json_str(0,"",rds);
                } catch (Exception e) {
                    content=dr_ajax_answer.sample_json_string(1,"error");
                }
                return content;
            }
        };
    }

    @FJW_URI(value ="dataItem/saveDataItem")
    public static DRHTTP_API saveDataItem(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                String dataItem_id=this.get_parameters(parameters,"dataItem_id");
                String channel_id=this.get_parameters(parameters,"channel_id");
                String slave_id=this.get_parameters(parameters,"slave_id");
                String register_type=this.get_parameters(parameters,"register_type");
                String data_index=this.get_parameters(parameters,"data_index");
                String address=this.get_parameters(parameters,"address");
                String label=this.get_parameters(parameters,"label");
                String data_type=this.get_parameters(parameters,"data_type");
                String coefficient=this.get_parameters(parameters,"coefficient");
                String negation=this.get_parameters(parameters,"negation");
                String BCD=this.get_parameters(parameters,"BCD");
                String ABS=this.get_parameters(parameters,"ABS");
                String base_value=this.get_parameters(parameters,"base_value");
                String introduction=this.get_parameters(parameters,"introduction");
                String data_table=this.get_parameters(parameters,"data_table");
                String field=this.get_parameters(parameters,"field");
                String value=this.get_parameters(parameters,"value");
                DRCDB db=drcdb;
                String sqlstr;
                boolean result=false;
                Date now=new Date();
                channel ch=null;
                for(int i=0;i<channels.size();i++){
                    if(channels.get(i).channel_id==Integer.parseInt(channel_id)){
                        ch=channels.get(i);
                        break;
                    }
                }
                if(ch!=null){
                    dataItem di=null;
                    if(dataItem_id==null||dataItem_id==""){
                        di=new dataItem(drcdb);
                    }else{
                        di=ch.dataItems.get(Integer.parseInt(dataItem_id));
                    }
                    if(di!=null){
                        di.channel_id=Integer.parseInt(channel_id);
                        di.slave_id=Integer.parseInt(slave_id);
                        di.register_type=Integer.parseInt(register_type);
                        di.data_index=Integer.parseInt(data_index);
                        di.address=Integer.parseInt(address);
                        di.label=label;
                        di.data_type=Integer.parseInt(data_type);
                        di.coefficient=Float.parseFloat(coefficient);
                        di.negation=Integer.parseInt(negation);
                        di.BCD=Integer.parseInt(BCD);
                        di.ABS=Integer.parseInt(ABS);
                        di.base_value=Float.parseFloat(base_value);
                        di.introduction=introduction;
                        di.data_table=data_table;
                        di.field=field;
                        di.value=Float.parseFloat(value);
                        di.create_time=now;
                        if(dataItem_id==null||dataItem_id==""){
                            ch.dataItems.add(di);
                        }
                        result=true;
                    }
                }
                // if(dataItem_id==null||dataItem_id==""){
                    // Date now=new Date();
                    // SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
                    // sqlstr="insert into dataItem(channel_id,slave_id,register_type,data_index,address,label,data_type,coefficient,negation,BCD,ABS,base_value,introduction,data_table,field,value,create_time) ";
                    // sqlstr+=String.format(" values(%s,%s,%s,%s,%s,'%s',%s,%s,%s,%s,%s,%s,'%s','%s','%s',%s,'%s')",
                    //     channel_id,slave_id,register_type,data_index,address,label,data_type,
                    //     coefficient,negation,BCD,ABS,base_value,introduction,data_table,field,value,ft.format(now));
                // }else{
                //     sqlstr="update dataItem";
                //     sqlstr+=String.format(" set slave_id=%s" , slave_id);
                //     sqlstr+=String.format(",register_type=%s" , register_type);
                //     sqlstr+=String.format(",data_index=%s" , data_index);
                //     sqlstr+=String.format(",address=%s" , address);
                //     sqlstr+=String.format(",label='%s'" , label);
                //     sqlstr+=String.format(",data_type=%s" , data_type);
                //     sqlstr+=String.format(",coefficient=%s" , coefficient);
                //     sqlstr+=String.format(",negation=%s" , negation);
                //     sqlstr+=String.format(",BCD=%s" , BCD);
                //     sqlstr+=String.format(",ABS=%s" , ABS);
                //     sqlstr+=String.format(",base_value=%s" , base_value);
                //     sqlstr+=String.format(",introduction='%s'" , introduction);
                //     sqlstr+=String.format(",data_table='%s'" , data_table);
                //     sqlstr+=String.format(",field='%s'" , field);
                //     sqlstr+=String.format(",value=%s" , value);
                //     sqlstr+=String.format(" where dataItem_id=%s" , dataItem_id);
                // }
                // result=db.executeUpdateSQL(sqlstr);
                if(result){
                    content=dr_ajax_answer.sample_json_string(0,"");
                }else{
                    content=dr_ajax_answer.sample_json_string(1,"保存失败！");
                }
                return content;
            }
        };
    }

    /**
     * 设定寄存器值，用于通过http调用
     * 返回值为json格式结果，包括code和msg
     * @param drcdb
     * @return
     */
    @FJW_URI(value ="dataItem/setDataItem")
    public static DRHTTP_API setDataItem(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                String slave_id=this.get_parameters(parameters,"slave_id");
                String register_type=this.get_parameters(parameters,"register_type");
                String address=this.get_parameters(parameters,"address");
                String value=this.get_parameters(parameters,"value");
                // String sqlstr="update dataItem";
                // sqlstr+=String.format(" set value=%s" , value);
                // sqlstr+=String.format(" where slave_id=%s" , slave_id);
                // sqlstr+=String.format(" and register_type=%s" , register_type);
                // sqlstr+=String.format(" and address=%s" , address);
                // boolean result=drcdb.executeUpdateSQL(sqlstr);
                boolean result=false;
                channel ch=channels.get(0);
                if(ch!=null){
                    dataItem di=ch.dataItems.get(Integer.parseInt(address));
                    if(di!=null){
                        if(di.register_type==Integer.parseInt(register_type)&&di.slave_id==Integer.parseInt(slave_id)&&di.address==Integer.parseInt(address)){
                            di.value=Float.parseFloat(value);
                            result=true;
                        }
                    }
                }
                if(result){
                    content=dr_ajax_answer.sample_json_string(0,"");
                }else{
                    content=dr_ajax_answer.sample_json_string(1,"保存失败！");
                }
                return content;
            }
        };
    }

    public static void main(String[] args){
        DRCDB drcdb=new DRCDB();
        // dataItem di=new dataItem(drcdb);
        // record_data_set rds=di.read_dataItems_page(1,1,3,10,1);
        // // for(Map<String, String> row:rds.datas){
        // //     System.out.println(row.get("dataItem"));
        // // }
        // System.out.println(record_data_set.get_answer_json_str(0,"",rds));
        byte[] result=dataItem.readLocalDataItem4bit( 1, 1, 0, 20);
        System.out.println(dr_util.bytes2hexstr(result));

    }
}